/* @(#)GSOperatorScanQueryAction.java
 *
 * Project: BUS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Simonyi Peng 2006-2-12    建立
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room,
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun,
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.util.ArrayList;

import system.LoginContext;

import com.hitrust.gs.bean.GSOrderBean;
import com.hitrust.gs.business.GSOrderProcessor;
import com.hitrust.gs.form.GSOrderSaveForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSOrderManufactureVO;
import com.hitrust.gs.vo.GSOrderVO;

import framework.core.LogWriter;

/**
 * @author simonyi
 *
 * 订单保存
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class GSOrderSaveAction extends GSBaseAction {

	public String execute(GSBaseForm aForm) throws Exception {

		LoginContext bean = (LoginContext) aForm.getSession().getAttribute(
				GSBaseConstants.LOGIN);

		GSOrderSaveForm tForm = (GSOrderSaveForm)aForm;

		GSOrderVO ordervo = new GSOrderVO();


		//ordervo.orderid = (String)request.getParameter("orderid");
		ordervo.orderCreateDate = tForm.getIsChange();
		ordervo.category = tForm.getCategory();
		ordervo.notes = tForm.getNotes();
		ordervo.balanceMode = "2".equals(tForm.getBalancemode())?tForm.getBalancemode_():tForm.getBalancemode();
		ordervo.deliverMode = "2".equals(tForm.getDelivermode())?tForm.getDelivermode_():tForm.getDelivermode();
		ordervo.deliverAddr = tForm.getDeliveraddr().split("%")[1];
		ordervo.orderDeptId = tForm.getDeptid().split("%")[0];
		ordervo.isReviewNeeded = tForm.getIsreviewneeded();
		ordervo.linkMan = tForm.getLinkman_()==null?"":tForm.getLinkman_();
		ordervo.linkTel = tForm.getLinktel_()==null?"":tForm.getLinktel_();

		ordervo.operid = bean.getLoginUserId();
		ordervo.orgid = bean.getLoginWarehouseCode();
		ordervo.balanceCategory = tForm.getBalancecategory();

		ArrayList ary = new ArrayList();

		String[] concentricityId = tForm.getConcentricityid();
		String[] manufactureId = tForm.getManufactureid();
		String[] orderCount = tForm.getOrdercount();
		String[] deliverDate = tForm.getDeliverdate();
		String[] deliverTime = tForm.getDelivertime();
		String[] manufactureUnitPrice = tForm.getManufactureunitprice();
		String[] providerId = tForm.getProviderid();

		String[] mode=tForm.getMode();
		try{
			for(int i=0;i<10;i++){
				GSOrderManufactureVO vo = new GSOrderManufactureVO();

				if(concentricityId[i] == null || concentricityId[i].length() == 0) {
					continue;
				}
				vo.concentricityId = concentricityId[i];
				vo.manufactureid = manufactureId[i];
				vo.ordercount = orderCount[i];
				vo.deliverdate = deliverDate[i];
				vo.delivertime = deliverTime[i];
				vo.manufactureUnitPrice = Double.parseDouble("".equals(manufactureUnitPrice[i])?"0":manufactureUnitPrice[i]);
				vo.providerId = providerId[i];
				vo.balanceCategory = ordervo.balanceCategory;
				vo.mode=mode[i];
				ary.add(vo);
			}

		}catch(Exception e){}

		GSOrderProcessor processor = new GSOrderProcessor();
		try{
			String orderId = processor.saveOrder(bean,ordervo,ary,"");//"010000512";

			try {
				processor.saveMyProduct(bean, ordervo, ary);
			} catch(Exception e){
				LogWriter.error(e.getLocalizedMessage(), e);
			}

			String saveOption = aForm.getRequest().getParameter("saveoption");
			aForm.getRequest().setAttribute("SUCCESSMSG","申购单保存成功，编号为 "+orderId);

			if("SA".equals(saveOption)){
				return "new";//允许录入新申购单
			}
			if("S".equals(saveOption)){
				ArrayList arylist = processor.getOrderDetail_(orderId);
				GSOrderBean tBean = processor.selectOrderByCode(orderId);

				aForm.getRequest().setAttribute("bean",tBean);
				aForm.getRequest().setAttribute("arylist",arylist);
				ArrayList bcList = GSBaseManager.getAllBalanceCategory();
				aForm.getRequest().setAttribute("balancecategory",bcList);

				return GSBaseConstants.FORWARD_SUCCESS;
			}
			return GSBaseConstants.FORWARD_FAILURE;
		}
		catch(GSException e){
			GSLogWriter.error(this, e.getMessage());
			aForm.getRequest().setAttribute("action", "A100101.do");
			throw e;
		}

	}
}
